﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;

namespace Qianwei.Web.Controls
{
    public partial class LeftMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetLeftMenu();
            }
        }

        private void SetLeftMenu()
        {
            string url = System.IO.Path.GetFileName(Request.Url.AbsolutePath);
            IList<menu_info> menulist = MenuInfoManager.Instance.QueryMenuInfo(null);
            StringBuilder strmenu = new StringBuilder();
            var curmenu = menulist.Where(r => r.Page_Url.Replace("/", "") == url && r.Parent_Id != 0);
            int curparentid = 0;
            if (curmenu.Count() > 0)
                curparentid = curmenu.First().Parent_Id;
            strmenu.Append("<ul>");
            foreach (var pie in menulist.Where(s => s.Parent_Id == curparentid))
            {
                strmenu.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                    pie.Page_Url, pie.Menu_Name);
            }
            strmenu.Append("</ul>");
            lmenu.Text = strmenu.ToString();
        }
    }
}